

class Solution:
    def findAnagrams(self, s: str, p: str) -> list[int]:
        if len(s)<len(p):
            return []
        middle={}
        res=[]
        middle2={}
        for i in range(len(p)):
            if p[i] not in middle:
                middle[p[i]]=0
            middle[p[i]]+=1
            middle2[p[i]]=0
        orther=0
        set_p=middle.keys()

        for i in range(len(p)):
            c=s[i]
            if c in set_p:
                middle2[c]+=1
            else:
                orther+=1
        if orther==0:
            if self.check(middle,middle2):
                res.append(0)
        for i in range(len(p),len(s)):
            c=s[i]
            old_c=s[i-len(p)]
            if c in set_p:
                middle2[c]+=1
            else:
                orther+=1
            if old_c in set_p:
                middle2[old_c]-=1
            else:
                orther-=1
            if orther==0 and self.check(middle,middle2):
                res.append(i-len(p)+1)
        return res
    def check(self, middle, middle2):
        for key in middle.keys():
            if middle[key]!=middle2[key]:
                return False
        return True




if __name__ == '__main__':
    Solution().findAnagrams("cbaebabacd",
                            "abc")


